package com.netease.nim.uikit.business.session.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.im.RecentContactHelper;
import com.aiyige.page.user.UserPage;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.widget.ShSwitchView;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.session.constant.Extras;
import com.netease.nim.uikit.common.util.sys.NetworkUtil;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.friend.FriendService;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserProfileActivity extends BaseActivity {

    String account;


    @BindView(R.id.nim_iv_head)
    ImageView nimIvHead;
    @BindView(R.id.nim_tv_name)
    TextView nimTvName;
    @BindView(R.id.switch_enable_sesshon_message_notification)
    ShSwitchView switchEnableSesshonMessageNotification;

    public static void start(Context context, String account) {
        Intent intent = new Intent();
        intent.setClass(context, UserProfileActivity.class);
        intent.putExtra(Extras.EXTRA_ACCOUNT, account);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_profile_activity);
        ButterKnife.bind(this);
        initTitleLayout(getString(R.string.chat_message));
        account = getIntent().getStringExtra(Extras.EXTRA_ACCOUNT);
        init();
    }

    private void init() {

        UserInfo userInfo = NimUIKit.getUserInfoProvider().getUserInfo(account);
        if (userInfo != null) {
            nimTvName.setText(userInfo.getName());
            GlideUtil.with(this).loadAvatar(userInfo.getAvatar()).into(nimIvHead);


        }
        boolean notice = NIMClient.getService(FriendService.class).isNeedMessageNotify(account);
        switchEnableSesshonMessageNotification.setOn(notice);
        switchEnableSesshonMessageNotification.setOnSwitchStateChangeListener(new ShSwitchView.OnSwitchStateChangeListener() {
            @Override
            public void onSwitchStateChange(final boolean isOn) {
                if (!NetworkUtil.isNetAvailable(UserProfileActivity.this)) {
                    Toast.makeText(UserProfileActivity.this, R.string.network_is_not_available, Toast.LENGTH_SHORT).show();
                    switchEnableSesshonMessageNotification.setOn(!isOn, true);
                    return;
                }
                NIMClient.getService(FriendService.class).setMessageNotify(account, isOn).setCallback(new RequestCallback<Void>() {
                    @Override
                    public void onSuccess(Void param) {
                        if (isOn) {
                            Toast.makeText(UserProfileActivity.this, "开启消息提醒成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(UserProfileActivity.this, "关闭消息提醒成功", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailed(int code) {
                        if (code == 408) {
                            Toast.makeText(UserProfileActivity.this, R.string.network_is_not_available, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(UserProfileActivity.this, "on failed:" + code, Toast.LENGTH_SHORT).show();
                        }
                        switchEnableSesshonMessageNotification.setOn(!isOn, true);
                    }

                    @Override
                    public void onException(Throwable exception) {
                        switchEnableSesshonMessageNotification.setOn(!isOn, true);
                    }
                });


            }
        });

    }

    @OnClick(R.id.ll_nim_head)
    public void onViewClicked() {
        RecentContactHelper.getInstance().getNimUserInfo(account, new RecentContactHelper.RequestUserIdCallback() {
            @Override
            public void onSuccess(String userId) {
                if (!TextUtils.isEmpty(userId)) {
                    UserPage.start(userId);
                }
            }
        });
    }
}
